
package com.skcc.cop.mrtaxi4driver.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skcc.cop.mrtaxi4driver.R;

/**
 * Title bar UI 을 담당하는 클래스
 * 
 * @author SK C&C
 * 
 */
public class TitleBar extends LinearLayout implements View.OnClickListener {
	 // ===========================================================
    // Constants
    // ===========================================================
    
    private static final String TAG = TitleBar.class.getSimpleName();
    
    private final static String BACK = "back";

    // ===========================================================
    // Fields
    // ===========================================================

    public OnTitleBarEventListener titlebarEventListener;
    
    private TextView titleTextview;
    private ImageView backImageview;
    
    private Context context;
    
    /**
     * 생성자
     * 
     * @param context
     * @param attrs
     */
    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        this.context = context;
        
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View titlebar = inflater.inflate(R.layout.view_titlebar, this, true);
        
        titleTextview = (TextView) titlebar.findViewById(R.id.titlebar_title_textview);
        backImageview = (ImageView) titlebar.findViewById(R.id.titlebar_back_imageview);
        backImageview.setTag(BACK);
        backImageview.setOnClickListener(this);
        
    }
    
    public void setTitle(String titleStr) {
    	titleTextview.setText(titleStr);
    }
    
    public void setTitle(int resId) {
    	String title = this.context.getResources().getString(resId);
    	titleTextview.setText(title);
    }
    
    public void showBack() {
    	backImageview.setVisibility(View.VISIBLE);
    }
    
    public void hideBack() {
    	backImageview.setVisibility(View.GONE);
    }
    
    /**
     * @param listener
     */
    public void setEventListener(OnTitleBarEventListener listener)
    {
    	titlebarEventListener = listener;
    }
    
    /**
     * callback 을 등록취소하는 메소드
     */
    public void removeEventListener()
    {
    	titlebarEventListener = null;
    }
    
    public void onClick(View v)
    {
        if (v.getTag().equals(BACK))
        {
        	titlebarEventListener.onBackClicked();
        } 
    }
    
    
    public interface OnTitleBarEventListener {
    	
        public void onBackClicked();
       
    }
}
